<?php

/**
 * ECTouch Open Source Project
 * ============================================================================
 * Copyright (c) 2012-2014 http://ectouch.cn All rights reserved.
 * ----------------------------------------------------------------------------
 * 文件名称：IndexController.class.php
 * ----------------------------------------------------------------------------
 * 功能描述：ECTouch首页控制器
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.ectouch.cn/docs/license.txt )
 * ----------------------------------------------------------------------------
 */
/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');

class IndexController extends CommonController {

    /**
     * 首页信息
     */
    public function index() {

        if($_SESSION['openid'])//添加openid
        {
            if(!$_GET['mshopNo'])
            {
                $where_No['uopenid']= $_SESSION['openid'];
                $user_No= $this->model->table('mshop_user_uopenid')
                ->field('mshop_id')
                ->where($where_No)
                ->find();
                $mshop_id=$user_No['mshop_id']?$user_No['mshop_id']:0;
                $mshop_id=$_SESSION['mshopNo']?$_SESSION['mshopNo']:$mshop_id;
                if($mshop_id)
                {

                  $r_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&mshopNo=".$mshop_id;
                  header('Location: '.$r_url);
                }

            }
        }

        $this->assign('title', '首页');
        $data=model('Index')->goods_list('all', 1);

        // print_r($data);exit;
        $this->assign('data',$data[0]);


        // C('page_size')

        // // 自定义导航栏
        // $navigator = model('Common')->get_navigator();
        // $this->assign('navigator', $navigator['middle']);
        // $this->assign('best_goods', model('Index')->goods_list('best', C('page_size')));
        // $this->assign('new_goods', model('Index')->goods_list('new', C('page_size')));
        // $this->assign('hot_goods', model('Index')->goods_list('hot', C('page_size')));
        // //首页推荐分类
        // $cat_rec = model('Index')->get_recommend_res();
        // $this->assign('cat_best', $cat_rec[1]);
        // $this->assign('cat_new', $cat_rec[2]);
        // $this->assign('cat_hot', $cat_rec[3]);
        // // 促销活动
        // $this->assign('promotion_info', model('GoodsBase')->get_promotion_info());
        // // 团购商品
        // $this->assign('group_buy_goods', model('Groupbuy')->group_buy_list(C('page_size'),1,'goods_id','ASC'));
        // // 获取分类
        // $this->assign('categories', model('CategoryBase')->get_categories_tree());
        // // 获取品牌
        // $this->assign('brand_list', model('Brand')->get_brands($app = 'brand', C('page_size'), 1));
        $this->display('index.dwt');


    }
    //新手必读
    public function user_new()
    {
         $this->assign('title', '新手必读');
         $this->display('user_new.dwt');
    }
   

   /**记录open_id**/
   public function write_openid()
   {
          //是推荐链接
        if($_SESSION['openid'])
        {
           //是否存在店铺
            $where_user['openid']= $_SESSION['openid'];
            $user_info = $this->model->table('users')
                ->field('mshop_id')
                ->where($where_user)
                ->find();

            if($user_info['mshop_id'])
            {
               $this->redirect(url('index/index'));
            }
            
            $where_up['uopenid']= $_SESSION['openid'];
            $where_up['status']=0;
            $user_uopen = $this->model->table('mshop_user_uopenid')
                ->field('mshop_id')
                ->where($where_up)
                ->find();
  
            if($user_uopen&&$_SESSION['mshopNo'])
            {
                 //会员已有店铺的优先级高

                $now_time=time();
                 $sql="delete from ".$this->model->pre."mshop_user_uopenid "
                ." where uopenid='$_SESSION[openid]'";
                 $result=$this->model->query($sql);              
           }
           $now_time=time();
           $sql="insert into ".$this->model->pre."mshop_user_uopenid(`mshop_id`,`uopenid`,`ctime`,`status`) "
                ."values('$_SESSION[mshopNo]','$_SESSION[openid]','$now_time','0')";

                // print_r($sql);exit;
           $result=$this->model->query($sql);

            $this->redirect(url('index/wxcode'));
            
        }
        else
        {
            $this->redirect(url('index/index'));
        }

         
   }
     /**
     * 公众号二维码
     */
     public function wxcode()
     {
         $this->assign('title','公众号二维码');
         $img_code=__URL__."/wxcode.jpg";
         if($_SESSION['mshopNo'])
         {
            $sql="select * from ".$this->model->pre."users where mshop_id=".$_SESSION['mshopNo'];
            $userinfo=$this->model->getRow($sql);

         }
         $this->assign('mshop_name',$userinfo['user_name']);
         $this->assign('img_code',$img_code);
         $this->assign('img_code',$img_code);
         $this->display('user_mshop_wxcode.dwt');
     }


    /**
     * ajax获取商品
     */
    public function ajax_goods() {
        if (IS_AJAX) {
            $type = I('get.type');
            $start = $_POST['last'];
            $limit = $_POST['amount'];
            $hot_goods = model('Index')->goods_list($type, $limit, $start);
            $list = array();
            // 热卖商品
            if ($hot_goods) {
                foreach ($hot_goods as $key => $value) {
                    $this->assign('hot_goods', $value);
                    $list [] = array(
                        'single_item' => ECTouch::view()->fetch('library/asynclist_index.lbi')
                    );
                }
            }
            echo json_encode($list);
            exit();
        } else {
            $this->redirect(url('index'));
        }
    }

}
